﻿
using ComponentFactory.Krypton.Navigator;
using ComponentFactory.Krypton.Toolkit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Cad
{
    public partial class SingleCAD : KryptonForm
    {
        private static SingleCAD _instance;
        private static MainForms _parent;
        public static SingleCAD getInstance(MainForms frm)
        {
            _parent = frm;
            if (_instance == null)
            {
                _instance = new SingleCAD();
            }
            //else
            //{
            //    _instance.Dispose();
            //    GC.SuppressFinalize(_instance);
            //    _instance = new SingleCAD();
            //}
            return _instance;
        }

        public SingleCAD()
        {
            InitializeComponent();
        }
        public SingleCAD(MainForms frm)
        {
            InitializeComponent();
            _parent = frm;
        }
        public KryptonPage BasePage;
        public TeighaBase DBForm;
        public string Dwgpath = "";

        private void SingleCAD_Load(object sender, EventArgs e)
        {
            DBForm = TeighaBase.getInstance(_parent);
        }

        public void LoadPage(string _Path,bool isReload=false)
        {
            //清空
            kryNavigaOne.Pages.Clear();
            DBForm = TeighaBase.getInstance(_parent);       
            DBForm.TopLevel = false;
            DBForm.Dock = DockStyle.Fill;
            DBForm.Activate();
      
            //DBForm.ContextMenuStrip = contextMenu;
            DBForm.FormBorderStyle = FormBorderStyle.None;
            DBForm.file_open_handlerNotBar(_Path);
            Dwgpath = _Path;
            try
            {
                if (DBForm.database != null)
                {
                    HostApplicationServices.WorkingDatabase = DBForm.database;
                    LayoutManager Layout = DBForm.lm;
                    if (DBForm.database.LayoutDictionaryId != null && DBForm.database.LayoutDictionaryId.IsValid)
                    {
                        using (DBDictionary layoutDict = (DBDictionary)DBForm.database.LayoutDictionaryId.GetObject(OpenMode.ForRead))
                        {
                            foreach (DBDictionaryEntry dicEntry in layoutDict)
                            {
                                KryptonPage page = new KryptonPage();
                                page.Name = dicEntry.Key;
                                page.Text = dicEntry.Key;
                                page.TextTitle = dicEntry.Key;
                                page.ToolTipBody = _Path;
                                page.ToolTipTitle = dicEntry.Key;
                                page.Select();
                                //Document pages cannot be docked or auto hidden
                                page.ClearFlags(KryptonPageFlags.DockingAllowAutoHidden | KryptonPageFlags.DockingAllowDocked);
                                kryNavigaOne.Pages.Add(page);
                                if (dicEntry.Key == Layout.CurrentLayout)
                                {
                                    page.Controls.Add(DBForm);
                                    DBForm.Show();
                                    BasePage = page;
                                    kryNavigaOne.SelectedPage = page;

                                }

                            }
                        }
                    }
                }

            }
            catch (Exception)
            {

            }

        }

        public void SetPageModel()
        {
            try
            {
                KryptonPageCollection pages = kryNavigaOne.Pages;
                if (pages.Count > 0)
                {
                    KryptonPage page = pages[0];
                    foreach (var item in pages)
                    {
                        if (page.Name == "Model")
                        {
                            page = item;
                            break;
                        }
                    }
                    if (page != null && BasePage != null && page.Name != BasePage.Name)
                    {
                        //DBForm = TeighaBase.getInstance();
                        HostApplicationServices.WorkingDatabase = DBForm.database;
                        LayoutManager Layout = DBForm.lm;
                      
                        //if (DBForm != null)
                        //{
                        //    DBForm.Invoker.Dispose();
                        //    if (DBForm.graphics != null)
                        //        DBForm.graphics.Dispose();
                        //    if (DBForm.helperDevice != null)
                        //        DBForm.helperDevice.Dispose();
                        //    if (DBForm.database != null)
                        //        DBForm.database.Dispose();
                        //}
                        Layout.CurrentLayout = page.Name;

                        BasePage.Controls.Remove(DBForm);
                        page.Controls.Add(DBForm);
                        BasePage = page;

                        DBForm.basepanel.Focus();
                    }
                }
            }
            catch { }
        }
        

        private void kryNavigaOne_SelectedPageChanged(object sender, EventArgs e)
        {
            try
            {
                //DBForm = TeighaBase.getInstance();
                KryptonPage page = kryNavigaOne.SelectedPage;
                if (page != null && BasePage != null && page.Name != BasePage.Name)
                {
                    HostApplicationServices.WorkingDatabase = DBForm.database;
                    LayoutManager Layout = DBForm.lm;
                    Layout.CurrentLayout = page.Name;
                    BasePage.Controls.Remove(DBForm);
                    page.Controls.Add(DBForm);
                    BasePage = page;

                    DBForm.basepanel.Focus();
                    DBForm.zoom_extents_handler();
                }
            }
            catch { }
        }

        #region 公共方法

        #endregion

        private void kryNavigaOne_Selecting(object sender, KryptonPageCancelEventArgs e)
        {       
            if (DBForm != null)
            {
                DBForm.zoom_extents_handler();
            }
        }

        #region 布设信息
        
        #endregion



    }
}
